package com.hzw.code.pageState

import android.content.Context
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.LayoutRes
import com.hzw.code.R
import com.hzw.code.pageState.State.*
import com.scwang.smartrefresh.layout.SmartRefreshLayout


/**
 *
 * @Author:         HuaZhongWei
 * @CreateDate:     2020/4/16 16:46
 *
 * @Description:    状态页
 *
 */

class PageStateLayout(context: Context) : FrameLayout(context) {

    private val viewMap = mutableMapOf<State, View>()

    private lateinit var pageStateManager: PageStateManager

    fun setStatusLayoutManager(pageStateManager: PageStateManager) = apply {
        this.pageStateManager = pageStateManager
        addAllLayoutToRootLayout()
    }

    /**
     * 添加布局到根Fragment上
     */
    private fun addAllLayoutToRootLayout() {
        // 添加内容界面
        if (pageStateManager.contentLayoutResId != 0) {
            addLayoutResId(pageStateManager.contentLayoutResId, CONTENT)
        }

        // 添加Loading界面
        if (pageStateManager.loadingLayoutResId != 0) {
            addLayoutResId(pageStateManager.loadingLayoutResId, LOADING)
        }

        // 添加空界面，这里添加的是ViewStub， 用的时候才inflate
        pageStateManager.emptyPageViewStub?.let {
            addView(it.viewStub)
        }

        // 添加错误界面，这里添加的是ViewStub， 用的时候才inflate
        pageStateManager.errorPageViewStub?.let {
            addView(it.viewStub)
        }

        // 添加网络错误界面，这里添加的是ViewStub， 用的时候才inflate
        pageStateManager.netErrorPageViewStub?.let {
            addView(it.viewStub)
        }
    }

    /**
     * 添加 Layout
     */
    private fun addLayoutResId(@LayoutRes layoutResId: Int, state: State) {
        val view = View.inflate(context, layoutResId, null)
        if (state == LOADING) {
            view.setOnClickListener(null)
            view.visibility = View.GONE
        }
        viewMap[state] = view
        addView(view)
    }

    /**
     * 隐藏Loading界面
     */
    fun hideLoading() {
        val view = viewMap[LOADING]
        view?.let {
            if (it.visibility == View.VISIBLE) {
                it.visibility = View.GONE
            }
        }
    }

    /**
     * 是否为加载中。。。
     */
    fun isLoading(): Boolean {
        val view = viewMap[LOADING]
        if (view != null) {
            return view.visibility == View.VISIBLE
        }
        return false
    }

    /**
     * 显示loading
     */
    fun showLoading() {
        val view = viewMap[LOADING]
        if (view != null) {
            showOrHideViewByState(LOADING)
        }
    }

    /**
     * 显示内容
     */
    fun showContent() {
        if (viewMap[LOADING] != null) {
            showOrHideViewByState(CONTENT)
        }
    }

    /**
     * 显示空数据，修改显示内容
     */
    fun showEmptyView(imageResId: Int = 0, tipText: String? = null, retryText: String? = null) {
        if (inflateLayout(EMPTY)) {
            showOrHideViewByState(EMPTY)
            changeViewContent(pageStateManager.emptyPageViewStub!!, viewMap[EMPTY]!!, imageResId, tipText, retryText)
        }
    }

    /**
     * 显示网络错误，修改显示内容
     */
    fun showNetErrorView(imageResId: Int = 0, tipText: String? = null, retryText: String? = null) {
        if (inflateLayout(NET_ERROR)) {
            showOrHideViewByState(NET_ERROR)
            changeViewContent(pageStateManager.netErrorPageViewStub!!, viewMap[NET_ERROR]!!, imageResId, tipText, retryText)
        }
    }

    /**
     * 显示错误界面，修改显示内容
     */
    fun showErrorView(imageResId: Int = 0, tipText: String? = null, retryText: String? = null) {
        if (inflateLayout(ERROR)) {
            showOrHideViewByState(ERROR)
            changeViewContent(pageStateManager.errorPageViewStub!!, viewMap[ERROR]!!, imageResId, tipText, retryText)
        }
    }

    /**
     * 修改相应的内容
     */
    private fun changeViewContent(
        pageViewStub: PageViewStub,
        view: View,
        imageResId: Int,
        tipText: String?,
        retryText: String?
    ) {
        if (imageResId != 0) {
            view.findViewById<View>(pageViewStub.imageViewId)?.let {imageView ->
                if (imageView is ImageView) {
                    imageView.setImageResource(imageResId)
                }
            }
        }

        tipText?.let {
            view.findViewById<View>(pageViewStub.tipsViewId)?.let { tipsView ->
                if (tipsView is TextView) {
                    tipsView.text = tipText
                }
            }
        }

        retryText?.let {
            view.findViewById<View>(pageViewStub.retryViewId)?.let { view ->
                if (view is TextView) {
                    view.text = retryText
                }
            }
        }
    }



    /**
     * 显示指定状态的View，隐藏其他状态
     */
    private fun showOrHideViewByState(state: State) {
        viewMap.forEach { item ->
            if (item.key == state) {
                item.value.visibility = View.VISIBLE
                pageStateManager.onShowHideViewListener?.onShowView(item.value, item.key)
            } else {
                if (item.value.visibility == View.VISIBLE) {
                    item.value.visibility = View.GONE
                }
                pageStateManager.onShowHideViewListener?.onHideView(item.value, item.key)
            }
        }
    }


    /**
     * 加载 网络错误，错误、空三种情况下的界面
     */
    private fun inflateLayout(state: State): Boolean {
        var isInflate = true
        if (viewMap[state] != null) {
            return isInflate
        }

        when (state) {
            NET_ERROR -> {
                if (pageStateManager.netErrorPageViewStub != null) {
                    val view = pageStateManager.netErrorPageViewStub!!.inflate()
                    viewMap[NET_ERROR] = view
                    initView(view, pageStateManager.netErrorPageViewStub!!)
                    isInflate = true
                } else {
                    isInflate = false
                }
            }
            ERROR -> {
                if (pageStateManager.errorPageViewStub != null) {
                    val view = pageStateManager.errorPageViewStub!!.inflate()
                    viewMap[ERROR] = view
                    initView(view, pageStateManager.errorPageViewStub!!)
                    isInflate = true
                } else {
                    isInflate = false
                }
            }
            EMPTY -> {
                if (pageStateManager.emptyPageViewStub != null) {
                    val view = pageStateManager.emptyPageViewStub!!.inflate()
                    viewMap[EMPTY] = view
                    initView(view, pageStateManager.emptyPageViewStub!!)
                    isInflate = true
                } else {
                    isInflate = false
                }
            }
            else -> {
            }
        }
        return isInflate
    }


    /**
     * 初始化页面显示内容
     */
    private fun initView(view: View, pageViewStub: PageViewStub) {
        val smartRefreshLayout = view.findViewById<SmartRefreshLayout>(R.id.smartRefreshLayout)
        smartRefreshLayout.setEnableRefresh(pageViewStub.isEnableRefresh)
        if (pageViewStub.isEnableRefresh) {
            smartRefreshLayout.setOnRefreshListener {
                pageViewStub.onRefreshListener?.onRefresh(it)
            }
        }

        view.findViewById<View>(pageViewStub.imageViewId)?.let {imageView ->
            if (imageView is ImageView && pageViewStub.imageResId != 0) {
                imageView.setImageResource(pageViewStub.imageResId)
            }
        }

        view.findViewById<View>(pageViewStub.tipsViewId)?.let { tipsView ->
            if (tipsView is TextView) {
                tipsView.text = pageViewStub.tipsText ?: ""
            }
        }


        view.findViewById<View>(pageViewStub.retryViewId)?.let {retryView ->
            if (retryView is TextView) {
                retryView.text = pageViewStub.retryText ?: ""
            }
            retryView.setOnClickListener {
                pageViewStub.onRetryListener?.onRetry()
            }
        }
    }

    fun recycler() {
        viewMap.clear()
    }
}